const router = require('express').Router();
const multer  = require('multer')
const path = require('path');
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './public/uploads')
  },
  filename: function (req, file, cb) {
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
    cb(null, file.fieldname + '-' + uniqueSuffix+ path.extname(file.originalname))
  }
})

const upload = multer({ storage: storage })

router.get('/upload', (req,res) => {
  res.render('upload')
})
// 上传文件路由
router.post('/upload', upload.single('img'), function (req, res) {
 res.send({
   code: 0,
   msg: '上传成功'
 })
})

module.exports = router;